<?php
/**
 * Created by PhpStorm.
 * User: ybjaa
 * Date: 2017/4/20
 * Time: 20:53
 */

namespace App\Extend;


class Regex
{


    /**
     * @param string $input 需要比较的字符串
     * @param string $pattern 正则表达式
     *
     * @return Boolean
     * true:匹配
     * false:没有匹配
     *
     *
     */
    public static function IsMatch(string $input,string $pattern)
    {
        $count= count(preg_grep($pattern,[$input]));
        return $count>0 ?true:false;
    }


    protected $pattern;

    /**
     *
     * 设置正则表达式
     *
     * @param string $pattern 正则表达式
     */
    public function setPattern(string $pattern){
        $this->pattern=$pattern;
    }

    /**
     * 获得正则表达式
     *
     * @return string 正则表达式
     *
     */
    public function getPattern()
    {
        return $this->pattern;
    }




    public function __construct(string $pattern="")
    {
        $this->pattern = $pattern;
    }


    /**
     * @param string $input 要匹配的字符串
     * @param string $pattern 正则表达式
     * @param string $replacement 准备更换的字符串
     *
     * @return
     * A new string that is identical to the input string, except that the replacement string takes the place of each matched string. If pattern is not matched in the current instance, the method returns the current instance unchanged.
     */
    static public function Replace(string $input, string $pattern, string $replacement)
    {
        return preg_replace($pattern,$replacement,$input);
       // return str_replace($pattern,$replacement,$input);
    }






}